package day4;

//克隆对象:
//浅克隆:需要实现一个Cloneable接口表示支持克隆，重写父类的Clone
//深克隆:需要实现一个Cloneable接口表示支持克隆，重写父类的Clone
public class Emp implements Cloneable {
    Integer id;
    String name;
    EmpInfo info;

    @Override
    protected Emp clone() throws CloneNotSupportedException {
        Emp e = (Emp) super.clone();
        e.info = info.clone();
        return e;
    }

    public Emp() {
        System.out.println("构造方法");
    }
}

class EmpInfo implements Cloneable {
    Integer id;
    String sex;
    Integer age;

    @Override
    protected EmpInfo clone() throws CloneNotSupportedException {
        return (EmpInfo) super.clone();
    }
}

class TestClone {
    public static void main(String[] args) throws CloneNotSupportedException {
        Emp e = new Emp();//走构造
        e.id = 100;
        e.name = "吴锡";
        e.info = new EmpInfo();
        e.info.id = 100;
        e.info.sex = "男";
        e.info.age = 18;
        Emp e2 = e.clone();//在堆内存直接复制一个对象出来
        System.out.println(e + " " + e.info);
        System.out.println(e2 + " " + e2.info);
        e.name = "wuxi";
        e.info.age = 20;
        System.out.println(e.name + " " + e.info.age);
        System.out.println(e2.name + " " + e2.info.age);
    }
}